﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUI.Controls
{
    public partial class EditInvoiceDetailsCtrl : BaseEditControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!SessionUser.Purchase)
            {
                txtUnitCost.Visible = false;
                lblUnitCost.Visible = false;
            }

            txtDiscount.ReadOnly = !SessionUser.EditSales;
            chkUsePercentage.Enabled = SessionUser.EditSales;
            txtUnitPrice.ReadOnly = !SessionUser.EditSales;
            txtUnitPrice.BackColor = SessionUser.EditSales ? System.Drawing.Color.White : System.Drawing.ColorTranslator.FromHtml("#b5d4ff");
            txtDiscount.BackColor = SessionUser.EditSales ? System.Drawing.Color.White : System.Drawing.ColorTranslator.FromHtml("#b5d4ff");
        }

        public string ProductName
        {
            get { return txtProductName.Text; }
            set { txtProductName.Text = value; }
        }

        public Decimal UnitPrice
        {
            get
            {
                return String.IsNullOrEmpty(txtUnitPrice.Text) ? 0 : Decimal.Parse(txtUnitPrice.Text);
            }
            set
            {
                txtUnitPrice.Text = value.ToString();
            }
        }

        public int Quantity
        {
            get
            {
                return String.IsNullOrEmpty(txtQuantity.Text) ? 0 : int.Parse(txtQuantity.Text);
            }
            set
            {
                txtQuantity.Text = value.ToString();
            }
        }

        public decimal Discount
        {
            get
            {
                if(String.IsNullOrEmpty(txtDiscount.Text))
                    return 0;
                if (!chkUsePercentage.Checked)
                    return decimal.Parse(txtDiscount.Text);
                else
                    return Math.Round(decimal.Parse(txtDiscount.Text) * Quantity * UnitPrice / 100, 2);
            }
            set
            {
                txtDiscount.Text = value.ToString();
            }
        }

        public Decimal GST
        {
            get
            {
                return String.IsNullOrEmpty(txtGST.Text) ? 0 : int.Parse(txtGST.Text);
            }
            set
            {
                txtGST.Text = value.ToString();
            }
        }

        public Decimal Total
        {
            get
            {
                return String.IsNullOrEmpty(txtTotal.Text) ? 0 : Decimal.Parse(txtTotal.Text);
            }
            set
            {
                txtTotal.Text = value.ToString();
            }
        }

        public Decimal UnitCost
        {
            get
            {
                return String.IsNullOrEmpty(txtUnitCost.Text) ? 0 : Decimal.Parse(txtUnitCost.Text);
            }
            set
            {
                txtUnitCost.Text = value.ToString();
            }
        }
    }
}